Imports System.IO
Imports System.Text
Imports System.Windows.Forms
Imports System.Web
Imports Devv.Core.Utils

Public Class Config

#Region "Properties"

	Private Shared _Encoding As Encoding = Encoding.UTF8
	Private Shared _AppName As String = String.Empty
	Private Shared _AppUrl As String = String.Empty
	Private Shared _EmailTo As String = String.Empty
	Private Shared _LogPath As String = "ErrorLogs"
	Private Shared _DoFileLog As Boolean = False
	Private Shared _DoEventLog As Boolean = False
	Private Shared _ExcludedCollectionKeywords As String() = {}

	Public Shared Property Encoding() As Encoding
		Get
			Return _Encoding
		End Get
		Set(ByVal Value As Encoding)
			_Encoding = Value
		End Set
	End Property

	Public Shared Property AppName() As String
		Get
			Return _AppName
		End Get
		Set(ByVal Value As String)
			_AppName = Value
		End Set
	End Property

	Public Shared Property AppUrl() As String
		Get
			Return _AppUrl
		End Get
		Set(ByVal Value As String)
			_AppUrl = Value
		End Set
	End Property

	Public Shared Property EmailTo() As String
		Get
			Return _EmailTo
		End Get
		Set(ByVal Value As String)
			If String.IsNullOrEmpty(Value) Or Value.IndexOf("@") > 0 Then
				_EmailTo = Value
			Else
				Throw New ErrooException(GetType(Config), My.Resources.Messages.ErrorInvalidEmail)
			End If
		End Set
	End Property

	Public Shared Property LogPath() As String
		Get
			Return _LogPath
		End Get
		Set(ByVal Value As String)

			If Not String.IsNullOrEmpty(Value) Then

				Dim strBasePath As String

				Value = Value.Replace("\", "/")

				If AssemblyUtil.GetAppType() = AppTypeEnum.AspNet Then
					strBasePath = HttpRuntime.AppDomainAppPath & "/"
				Else
					strBasePath = Path.GetDirectoryName(Application.ExecutablePath) & "/"
				End If

				If Not IOUtil.ValidatePhysicalPath(Value) Then
					Value = strBasePath & Value
				End If

				If Value.Substring(Value.Length - 1, 1) <> "/" Then
					Value += "/"
				End If

				If IOUtil.ValidatePhysicalPath(Value) Then
					_LogPath = Value
				End If

			End If

		End Set
	End Property

	Public Shared Property DoFileLog() As Boolean
		Get
			Return _DoFileLog
		End Get
		Set(ByVal Value As Boolean)
			_DoFileLog = Value
		End Set
	End Property

	Public Shared Property DoEventLog() As Boolean
		Get
			Return _DoEventLog
		End Get
		Set(ByVal Value As Boolean)
			_DoEventLog = Value
		End Set
	End Property

	Public Shared Property ExcludedCollectionKeywords() As String()
		Get
			Return _ExcludedCollectionKeywords
		End Get
		Set(ByVal value As String())
			_ExcludedCollectionKeywords = value
		End Set
	End Property

#End Region

#Region "Load configuration"

	Public Shared Sub LoadFromXml()

		Dim strEncoding As String = ConfigUtil.GetConfig("Devv.Core.Erroo.Encoding", String.Empty)

		If String.IsNullOrWhiteSpace(strEncoding) Then
			strEncoding = ConfigUtil.GetConfig("Devv.Encoding", "UTF-8")
		End If

		Encoding = Encoding.GetEncoding(strEncoding)

		AppName = ConfigUtil.GetConfig("Devv.Core.Erroo.AppName", _AppName)
		AppUrl = ConfigUtil.GetConfig("Devv.Core.Erroo.AppUrl", _AppUrl)

		If String.IsNullOrEmpty(AppName) Then
			AppName = ConfigUtil.GetConfig("Devv.AppName", _AppName)
		End If

		If String.IsNullOrEmpty(AppUrl) Then
			AppUrl = ConfigUtil.GetConfig("Devv.AppUrl", _AppUrl)
		End If

		EmailTo = ConfigUtil.GetConfig("Devv.Core.Erroo.EmailTo", _EmailTo)
		LogPath = ConfigUtil.GetConfig("Devv.Core.Erroo.LogPath", _LogPath)
		DoFileLog = ConfigUtil.GetConfig("Devv.Core.Erroo.DoFileLog", _DoFileLog)
		DoEventLog = ConfigUtil.GetConfig("Devv.Core.Erroo.DoEventLog", _DoEventLog)

		Dim keywords As String = ConfigUtil.GetConfig("Devv.Core.Erroo.ExcludedCollectionKeywords", "__,password")

		If keywords.Length > 1 Then

			ExcludedCollectionKeywords = keywords.Split(",".ToCharArray())

		End If

	End Sub

#End Region

End Class